package br.usp.icmc.biomac.client;

import java.util.ArrayList;

import br.usp.icmc.biomac.client.view.DicomResultCellBrowser;
import br.usp.icmc.biomac.client.view.DicomResultCellTable;
import br.usp.icmc.biomac.client.view.DicomSearchWidget;
import br.usp.icmc.biomac.client.view.ImageAnnotation;
import br.usp.icmc.biomac.client.view.ImageListView;
import br.usp.icmc.biomac.client.view.ImageView;
import br.usp.icmc.biomac.client.view.ROIAnnotationCellTable;
import br.usp.icmc.biomac.client.view.ROIImageCellTable;
import br.usp.icmc.biomac.client.view.impl.DicomResultCellBrowserImpl;
import br.usp.icmc.biomac.client.view.impl.DicomResultCellTableImpl;
import br.usp.icmc.biomac.client.view.impl.DicomSearchWidgetImpl;
import br.usp.icmc.biomac.client.view.impl.ImageAnnotationImpl;
import br.usp.icmc.biomac.client.view.impl.ImageListViewImpl;
import br.usp.icmc.biomac.client.view.impl.ImageViewImpl;
import br.usp.icmc.biomac.client.view.impl.ROIAnnotationCellTableImpl;
import br.usp.icmc.biomac.client.view.impl.ROIImageCellTableImpl;
import br.usp.icmc.biomac.shared.ImageDicom;
import br.usp.icmc.biomac.shared.Line;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.biomac.shared.Rectangle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.view.client.ListDataProvider;

public class ClientFactoryImpl implements ClientFactory {
	
	private static final EventBus eventBus = new SimpleEventBus();
	private static final PlaceController placeController = new PlaceController(eventBus);
	private static final Web_padServiceAsync rpcService = GWT.create(Web_padService.class);
	private static final RootWidget rootWidget = new RootWidget();
	// Create a data provider for ROIs.
	private static final ListDataProvider<ROI> dataProvider = new ListDataProvider<ROI>();
	
	private ArrayList<ImageDicom> IMAGESDICOM = new ArrayList<ImageDicom>();

	private static final ROIImageCellTable roiimagecelltable = new ROIImageCellTableImpl(dataProvider);
	private static final ROIImageCellTable roiimagecelltablePlace = new ROIImageCellTableImpl(dataProvider);
	private static final ROIAnnotationCellTable roiannotationcelltable = new ROIAnnotationCellTableImpl(dataProvider);
	private static final ROIAnnotationCellTable roiannotationcelltablePlace = new ROIAnnotationCellTableImpl(dataProvider);
	private static final DicomSearchWidget dicomSearchWidget = new DicomSearchWidgetImpl();
	private static final DicomResultCellBrowser dicomResultCellBrowser = new DicomResultCellBrowserImpl();
	private static final DicomResultCellTable dicomResultCellTable = new DicomResultCellTableImpl();
	private static final ImageAnnotation imageAnnotation = new ImageAnnotationImpl(roiannotationcelltable);
	private static final ImageAnnotation imageAnnotationPlace = new ImageAnnotationImpl(roiannotationcelltablePlace);

	private static ImageView imageView;
	private static ImageView imageViewPlace;
	private static ImageListView imageListView;
	
	public static class RootWidget implements AcceptsOneWidget{
		// Create a tab panel with three tabs, each of which displays a different
	    // piece of text.
	    ScrollPanel tp = new ScrollPanel();

	    public RootWidget(){
			RootLayoutPanel.get().clear();
			RootLayoutPanel.get().add(tp);
	    }
	    
		@Override
		public void setWidget(IsWidget w) {
			if (w== null) return;
			tp.add(w);
		}
	}
	
	@Override
	public EventBus getEventBus()
	{
		return eventBus;
	}

	@Override
	public PlaceController getPlaceController()
	{
		return placeController;
	}
	
	@Override
	public Web_padServiceAsync getRpcService() {
		return rpcService;
	}
	
	@Override
	public RootWidget getRootWidget() {
		return rootWidget;
	}

	@Override
	public ImageView getImageView() {
		if(imageView == null){
			imageView = new ImageViewImpl(roiimagecelltable, imageAnnotation);
		}
		return imageView;
	}

	@Override
	public ImageListView getImageListView() {
		if (imageListView == null)
			imageListView = new ImageListViewImpl(dicomSearchWidget, dicomResultCellTable, dicomResultCellBrowser,getImageView());
		return imageListView;
	}

	@Override
	public DicomResultCellTable getDicomResultCellTable() {
		return dicomResultCellTable;
	}

	@Override
	public ImageAnnotation getImageAnnotation() {
		return imageAnnotation;
	}

	@Override
	public DicomSearchWidget getDicomSearchWidget() {
		return dicomSearchWidget;
	}

	@Override
	public ImageView getImagePlaceView() {
		if(imageViewPlace == null)
			imageViewPlace = new ImageViewImpl(roiimagecelltablePlace, imageAnnotationPlace);
		return imageViewPlace;
	}

	@Override
	public ImageAnnotation getImageAnnotationPlace() {
		return imageAnnotationPlace;
	}

	@Override
	public ArrayList<ImageDicom> getImageDicom() {
		return this.IMAGESDICOM;
	}

	@Override
	public void setImageDicom(ArrayList<ImageDicom> imageDicoms) {
		this.IMAGESDICOM = imageDicoms;
	}

	@Override
	public void addROIImageDicom(int indexImage, ROI roi) {
		if (roi instanceof Line) {
			this.IMAGESDICOM.get(indexImage).addROI(roi);
		}
		if (roi instanceof Rectangle) {
			this.IMAGESDICOM.get(indexImage).addROI(roi);
		}
		// TODO add new draw features
	}

	@Override
	public void rmvROIImageDicom(int indexImage, int indexROI) {
		this.IMAGESDICOM.get(indexImage).rmvROI(indexROI);
	}

	@Override
	public DicomResultCellBrowser getDicomResultCellBrowser() {
		return dicomResultCellBrowser;
	}
}
